//
//  ConfigurationViewController.m
//  Voxper
//
//  Created by Irfan Bashir on 4/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ConfigurationViewController.h"
#import "VoxperAppDelegate.h"
#import "UIManager.h"
#import "Settings.h"
#import "SipManager.h"
@interface ConfigurationViewController(Private)
- (void)saveConfigurations:(id)sender;
@end

@implementation ConfigurationViewController

- (void)saveConfigurations:(id)sender {
    if ([[app.uiManager.settings getUserName] length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SIP" message:@"Re-start the application." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    [app.uiManager.settings setServerName:m_txtServerName.text];
    [app.uiManager.settings setUserName:m_txtUserName.text];
    [app.uiManager.settings setPassword:m_txtPassword.text];
    [app.uiManager.settings setServerPort:m_txtPort.text];
    [self.navigationController popViewControllerAnimated:YES];
    //[app.manager disconnect];
    [app initiateUI];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        app = [UIApplication sharedApplication].delegate;
    }
    return self;
}

- (void)dealloc
{
    [m_txtUserName release];
    [m_txtPassword release];
    [m_txtServerName release];
    [m_txtPort release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    m_txtServerName.text = [app.uiManager.settings getServerName];
    m_txtUserName.text = [app.uiManager.settings getUserName];
    m_txtPassword.text = [app.uiManager.settings getPassword];
    m_txtPort.text = [app.uiManager.settings getServerPort];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleBordered target:self action:@selector(saveConfigurations:)] autorelease];
}

- (void)viewDidUnload
{
    [m_txtUserName release];
    m_txtUserName = nil;
    [m_txtPassword release];
    m_txtPassword = nil;
    [m_txtServerName release];
    m_txtServerName = nil;
    [m_txtPort release];
    m_txtPort = nil;
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

@end
